//
//  SelfAlertView.m
//  kongfangxiong
//
//  Created by 李小龙 on 2017/6/29.
//  Copyright © 2017年 lixiaolong. All rights reserved.
//

#import "SelfAlertView.h"

@implementation SelfAlertView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (void)awakeFromNib {
    [super awakeFromNib];
    [self loadView];
}

- (instancetype)init {
    self = [super init];
    if (self) {
        [self setFrame:Screen];
        [self loadView];
    }
    return self;
}
- (void)loadView {
    self.view = [[[NSBundle mainBundle] loadNibNamed:@"SelfAlertView" owner:self options:nil] lastObject];
    self.view.frame = self.bounds;
    [self addSubview:self.view];
    
    self.alpha = 0;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hidden:)];
    [self.shadowView addGestureRecognizer:tap];
    [[[UIApplication sharedApplication] keyWindow] addSubview:self];
    
//    [self show];
}
- (IBAction)buttonSureAction:(id)sender {
    [self hidden:nil];
    if (self.block) {
        self.block(alertTypeSure);
    }
}
- (IBAction)buttonCancelAction:(id)sender {
    [self hidden:nil];
    if (self.block) {
        self.block(alertTypeCancel);
    }
}

- (void)hidden:(UIGestureRecognizer *)tap {
    [UIView animateWithDuration:0.3 animations:^{
        [self setAlpha:0];
    } completion:^(BOOL finished) {
        self.hidden = YES;
        [self removeFromSuperview];
    }];
    if (tap) {
        // 取消
        if (self.block) {
            self.block(alertTypeCancel);
        }
    }
}
- (void)show {
    [UIView animateWithDuration:0.4 animations:^{
        [self setAlpha:1];
    } completion:^(BOOL finished) {
        self.hidden = NO;
    }];
}

@end
